import pingpp
from pingpp import api_requestor
import time
from Crypto.PublicKey import RSA
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from base64 import b64encode
import base64
def get_rsa_verify_data(body, uri, timestamp):
    verify_data = [body] if body else []
    verify_data.extend([uri, repr(timestamp)])
    return "".join(verify_data)

def rsa_sign(private_key, data):
    
    rsa_key = RSA.importKey(private_key)
    signer = PKCS1_v1_5.new(rsa_key)
    digest = SHA256.new(data.encode())
    sign = signer.sign(digest)
    return b64encode(sign)

def decode_base64(data):
    remainder = len(data) % 4
    if remainder:
        data += '=' * (4 - remainder)
    return base64.decodestring(data.encode('utf-8'))

def verify(data, sig):
    signs = decode_base64(sig)
    data = data.decode('utf-8') if hasattr(data, "decode") else data
    public_key_path = 'ping_rsa_public_key.pem'
    public_key = open(public_key_path,'r').read().strip()
    pubkey = RSA.importKey(public_key)
    digest = SHA256.new(data.encode('utf-8'))
    print(digest.hexdigest())
    #print(signs)
    pkcs = PKCS1_v1_5.new(pubkey)
    return pkcs.verify(digest, signs)

# 示例

data = '''{"id":"evt_401171124195717652755003","created":1511524637,"livemode":true,"type":"order.succeeded","data":{"object":{"id":"2011711240008223371","object":"order","created":1511524623,"livemode":true,"paid":true,"refunded":false,"status":"paid","app":"app_bvXL44z1q9GGWjz9","uid":"4cb6b8a8-742f-4c5b-9a59-bddb13b50308","available_balance":0,"merchant_order_no":"20171124002808571009","type":"purchase","amount":100,"actual_amount":100,"amount_refunded":0,"amount_paid":100,"extra_amount":0,"coupon_amount":0,"balance_amount":0,"charge_amount":100,"currency":"cny","subject":"test","body":"body","client_ip":"127.0.0.1","time_paid":1511524636,"time_expire":1511611023,"refunds":{"object":"list","url":"/v1/orders/2011711240008223371/refunds","has_more":false,"data":[]},"charge":"ch_TSK88OHqn584zzTqXPynTy58","balance_transaction":null,"coupon":null,"description":"odrecharge","metadata":{},"charge_essentials":{"channel":"isv_wap","transaction_no":"4200000025201711246793633615","failure_code":null,"failure_msg":null,"extra":{"pay_channel":"wx","terminal_id":"00000001","result_url":"http://www.proorder.cn/buffet/index.html#/pages/home/account?ordercode=33D13B8855452B56EC6892EF098C3260","channel_discount":0,"buyer_account":"ok3GRuEF5pJh82wp0VGna4AlhjNM","merchant_discount":0},"credential":{}},"user_fee":0,"user_from":null,"receipt_app":"app_4ar1iPirnDK01y1a","service_app":"app_4ar1iPirnDK01y1a","available_methods":[],"charges":{"object":"list","url":"/v1/charges","has_more":false,"data":[{"id":"ch_TSK88OHqn584zzTqXPynTy58","object":"charge","created":1511524623,"livemode":true,"paid":true,"refunded":false,"reversed":false,"app":"app_4ar1iPirnDK01y1a","channel":"isv_wap","order_no":"20171124002808571009","client_ip":"127.0.0.1","amount":100,"amount_settle":100,"currency":"cny","subject":"test","body":"body","extra":{"pay_channel":"wx","terminal_id":"00000001","result_url":"http://www.proorder.cn/buffet/index.html#/pages/home/account?ordercode=33D13B8855452B56EC6892EF098C3260","channel_discount":0,"buyer_account":"ok3GRuEF5pJh82wp0VGna4AlhjNM","merchant_discount":0},"time_paid":1511524636,"time_expire":1511611023,"time_settle":null,"transaction_no":"4200000025201711246793633615","refunds":null,"amount_refunded":0,"failure_code":null,"failure_msg":null,"metadata":{},"credential":{},"description":"odrecharge"}]}}},"object":"event","request":"iar_fzjbvD8SSO4CazjvjHOuLCmH","pending_webhooks":0}'''

sign = "jvESXe5egOEF0defYjQfQiB8sgu1NCNOVKxmHbDFto4AGrwLEn+f/m/l+oSIt322+oicZ0ekb853uszAu06GFZTJ+PsgpqW+u6vkjZ0nP2BD1YifcJ6TqkXlyHZHZRURtTgQ2qq4JIe18Z4Ie9q3z2ZXrk6fYzhzqNANxsFMT3hlCQNzfWExkxxqP20CcaXR5/cXySr9OK4YLcEQHJBNWQwWlRRfLoDMn/Qlu7azb79WU84G+xCA1v1MZX+L1RNr1s5g0L1Nf+TM2Ep7lBXlOdbwqM+ZB4nPtQoIPzKF0OOwIbqFz50riBiPl0TVamf4MFHXG/08zeYZou7kOXbF7g=="
print(len(data))
# 验证
if verify(data, sign):
    print("Signature verification succeeded")
else:
    print("Signature verification failed")

